package com.tom.demo.controller.system;

import com.tom.demo.common.annotation.OperaLog;
import com.tom.demo.common.enumeration.OperaTypeEnum;
import com.tom.demo.common.response.Result;
import com.tom.demo.dto.AssignRoleMenuDto;
import com.tom.demo.dto.AssignUserRoleDto;
import com.tom.demo.dto.RoleEditDto;
import com.tom.demo.dto.RoleSearchDto;
import com.tom.demo.service.system.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = {"角色管理"})
@RestController
@RequestMapping(path = {"/role"})
public class RoleController {

    @Resource
    private RoleService roleService;

    @PostMapping(path = {"/pageList"})
    @ApiOperation(value = "获取角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页查询多少条", defaultValue = "5", dataType = "int", paramType = "query")
    })
    @PreAuthorize(value = "hasAuthority('system:role:list')")
    public Result pageList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           @RequestBody RoleSearchDto roleSearchDto){
        return roleService.pageList(page,pageSize,roleSearchDto);
    }


    @PutMapping(path = {"/updateRoleStatus"})
    @ApiOperation(value = "修改角色状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "角色状态", dataType = "Boolean", paramType = "query")
    })
    @OperaLog(operaModel = "角色管理",operaType = OperaTypeEnum.UPDATE,operaDesc = "修改角色状态")
    public Result updateRoleStatus(@RequestParam(value = "roleId")String roleId,
                           @RequestParam(value = "status")Boolean status){
        return roleService.updateRoleStatus(roleId,status);
    }

    @GetMapping(path = {"/findByRoleId/{roleId}"})
    @ApiOperation(value = "根据角色id获取角色信息回显")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String", paramType = "path")
    public Result findByRoleId(@PathVariable(value = "roleId")String roleId){
        return roleService.findByRoleId(roleId);
    }

    @DeleteMapping(path = {"/deleteByRoleId/{roleId}"})
    @ApiOperation(value = "根据用户id删除用户信息")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String", paramType = "path")
    @OperaLog(operaModel = "角色管理",operaType = OperaTypeEnum.DELETE,operaDesc = "根据用户id删除用户信息")
    public Result deleteByRoleId(@PathVariable(value = "roleId")String roleId){
        return roleService.deleteByRoleId(roleId);
    }

    @PostMapping(path = {"/insertRole"})
    @ApiOperation(value = "添加角色")
    @OperaLog(operaModel = "角色管理",operaType = OperaTypeEnum.ADD,operaDesc = "添加角色")
    public Result insertRole(@RequestBody @Validated(value = RoleEditDto.Insert.class) RoleEditDto roleEditDto){
        return roleService.insertRole(roleEditDto);
    }

    @PutMapping(path = {"/updateRole"})
    @ApiOperation(value = "修改角色")
    @OperaLog(operaModel = "角色管理",operaType = OperaTypeEnum.UPDATE,operaDesc = "修改角色")
    public Result updateRole(@RequestBody @Validated(value = RoleEditDto.Update.class) RoleEditDto roleEditDto) {
        return roleService.updateRole(roleEditDto);
    }


    @GetMapping(path = {"/toAssign/{roleId}"})
    @ApiOperation(value = "获取当前角色id获取所拥有的菜单列表")
    public Result toAssign(@PathVariable(value = "roleId")String roleId){
        return roleService.toAssign(roleId);
    }

    @PostMapping(path = {"/doAssign"})
    @ApiOperation(value = "为角色分配菜单")
    @OperaLog(operaModel = "角色管理",operaType = OperaTypeEnum.UPDATE,operaDesc = "为角色分配菜单")
    public Result doAssign(@RequestBody AssignRoleMenuDto assignRoleMenuDto){
        return roleService.doAssign(assignRoleMenuDto);
    }


}
